package net.ossrs.yasea.demo.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.faucamp.simplertmp.RtmpHandler;

import net.ossrs.yasea.SrsEncodeHandler;
import net.ossrs.yasea.SrsPublisher;
import net.ossrs.yasea.SrsPublisher1;
import net.ossrs.yasea.SrsRecordHandler;
import net.ossrs.yasea.demo.R;
import net.ossrs.yasea.demo.util.ImageDispose;

import java.io.IOException;
import java.net.SocketException;

/**
 * @version 1.0
 * @Description:
 * @Author: yong
 * @time 2021/5/25 0025 16:38
 */
public class T extends AppCompatActivity implements SrsEncodeHandler.SrsEncodeListener, RtmpHandler.RtmpListener, SrsRecordHandler.SrsRecordListener {
    /**
     * 开始推流
     */
    private Button btnStart;
    /**
     * 结束推流
     */
    private Button btnEnd;

    /**
     * srs推流
     */
    private SrsPublisher1 mPublisher;

    /**
     * 推流地址
     */
    private String rtmpUrl = "rtmp://192.168.0.124:1935/live/livestream";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t);

        initDate();
        initView();
    }

    private void initDate() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap =BitmapFactory.decodeResource(getResources(),R.mipmap.bg,options);

        //获取图片的宽高
        int height = options.outHeight;
        int width = options.outWidth;

        Log.i("wk","图片的宽度:"+width+"图片的高度:"+height);
        final byte[] bytes = ImageDispose.Bitmap2Bytes(bitmap);

        mPublisher = new SrsPublisher1();
        mPublisher.setEncodeHandler(new SrsEncodeHandler(this));
        mPublisher.setRtmpHandler(new RtmpHandler(this));
        mPublisher.setRecordHandler(new SrsRecordHandler(this));
        mPublisher.setSendVideoOnly(true);
        mPublisher.setVideoHDMode();
        mPublisher.startPublish(rtmpUrl);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    mPublisher.startEncode(bytes,640,360);
                }
            }
        }).start();

        Log.e("Yong", "" + bytes.length);
    }

    private void initView() {
        btnStart = findViewById(R.id.btn_start);
        btnEnd = findViewById(R.id.btn_end);
    }

    @Override
    public void onNetworkWeak() {

    }

    @Override
    public void onNetworkResume() {

    }

    @Override
    public void onEncodeIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRtmpConnecting(String msg) {

    }

    @Override
    public void onRtmpConnected(String msg) {

    }

    @Override
    public void onRtmpVideoStreaming() {

    }

    @Override
    public void onRtmpAudioStreaming() {

    }

    @Override
    public void onRtmpStopped() {

    }

    @Override
    public void onRtmpDisconnected() {

    }

    @Override
    public void onRtmpVideoFpsChanged(double fps) {

    }

    @Override
    public void onRtmpVideoBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpAudioBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpSocketException(SocketException e) {

    }

    @Override
    public void onRtmpIOException(IOException e) {

    }

    @Override
    public void onRtmpIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRtmpIllegalStateException(IllegalStateException e) {

    }

    @Override
    public void onRecordPause() {

    }

    @Override
    public void onRecordResume() {

    }

    @Override
    public void onRecordStarted(String msg) {

    }

    @Override
    public void onRecordFinished(String msg) {

    }

    @Override
    public void onRecordIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRecordIOException(IOException e) {

    }
}
